/** 
 * UIMA Connectors
 * Copyright (C) 2010  Nicolas Hernandez
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package fr.univnantes.lina.uima.connectors.ftb;

import java.util.HashMap;
import java.util.Map;

import fr.univnantes.lina.uima.connectors.tagset.TagsetAnalyser;

/**
 * Implementation of the TagsetAnalyser for the ftb
 * 
 * @author hernandez
 *
 */
public class FTBPOSTagAnalyser_Impl implements TagsetAnalyser {

	
	/*
	 * LOCAL VARIABLES
	 */

	/**
	 * keys
	 * 
	 */

	private static String cat = "cat";
	private static String subcat = "subcat";
	private static String mph = "mph";
	private static String gender = "gender";
	private static String number = "number";
	private static String mood = "mood";
	private static String person = "person";
	private static String possNumber = "possNumber";
	
	
	/**
	 * Analyse the ftb morpho-syntax features of a word
	 * See ftb "Guide des mots simples et composés" for the tag meaning (annexes)
	 * Take in parameters String variables which corresponds to the  morpho-syntax description of a tag set for a word
	 * @see fr.univnantes.lina.uima.connectors.corpus.ftb.POSTagsetMapping_Interface#analyseTag(java.lang.Object[])
	 * @return a map of detailed morpho-syntax features
	 */
	@Override
	public Map<String, Object> analyseTag (Map<String, Object> wordMorphoSyntaxFeatures) {

		
		wordMorphoSyntaxFeatures.put(mood, "");
		wordMorphoSyntaxFeatures.put(gender, "");
		wordMorphoSyntaxFeatures.put(number, "");
		wordMorphoSyntaxFeatures.put(person, "");
		wordMorphoSyntaxFeatures.put(possNumber, ""); // mon, notre


		// Get the Verb features
		// V verb
		// mode							ftb+ mode			nh mode			ftb+ tag	nh ftb+ tag	
		// C présent conditionnel		conditionnel		conditionnel	?			V
		// F futur indicatif			indicatif			futur			V
		// G participe présent			participe présent	présent			VPR
		// I imparfait indicatif		indicatif			passé			V
		// J passé simple indicatif		indicatif			passé			V
		// K participe passé			participe passé		passé			VPP
		// P présent indicatif			indicatif			présent			V
		// S présent subjonctif			subjonctif			présent			VS
		// T imparfait subjonctif		subjonctif			passé			VS
		// W infinitif					infinitif			unknown			VINF
		// Y impératif					impératif			présent			VIMP
		// * Ai regardé les data/lexicon dans https://gforge.inria.fr/frs/download.php/27240/melt-0.6.tar.gz
		// person 1, 2, 3
		// number s, p
		// gender m, f
		if (((String)wordMorphoSyntaxFeatures.get(cat)).equalsIgnoreCase("V")) {	
			// Get the mode
			if (!((String)wordMorphoSyntaxFeatures.get(mph)).equalsIgnoreCase("")) wordMorphoSyntaxFeatures.put(mood, ((String)wordMorphoSyntaxFeatures.get(mph)).substring(0, 1)); 

			// Get the person 
			// Get the number
			if ((((String)wordMorphoSyntaxFeatures.get(mph)).length() == 3) 
					&& (((String)wordMorphoSyntaxFeatures.get(mood)).equalsIgnoreCase("P") 
							|| ((String)wordMorphoSyntaxFeatures.get(mood)).equalsIgnoreCase("F") 
							|| ((String)wordMorphoSyntaxFeatures.get(mood)).equalsIgnoreCase("J") 
							|| ((String)wordMorphoSyntaxFeatures.get(mood)).equalsIgnoreCase("C") 
							|| ((String)wordMorphoSyntaxFeatures.get(mood)).equalsIgnoreCase("I") 
							|| ((String)wordMorphoSyntaxFeatures.get(mood)).equalsIgnoreCase("S")
							||((String) wordMorphoSyntaxFeatures.get(mood)).equalsIgnoreCase("T")
							|| ((String)wordMorphoSyntaxFeatures.get(mood)).equalsIgnoreCase("Y")) ) {
				wordMorphoSyntaxFeatures.put(person,((String)wordMorphoSyntaxFeatures.get(mph)).substring(1, 2)); // 1, 2, 3
				wordMorphoSyntaxFeatures.put(number,((String)wordMorphoSyntaxFeatures.get(mph)).substring(2, 3)); // s, p
			}

			// Get the gender
			// Get the number
			if ((((String)wordMorphoSyntaxFeatures.get(mph)).length() == 3) 
					&& (((String)wordMorphoSyntaxFeatures.get(mood)).equalsIgnoreCase("K"))) {
				wordMorphoSyntaxFeatures.put(gender,((String)wordMorphoSyntaxFeatures.get(mph)).substring(1, 2)); // m, f
				wordMorphoSyntaxFeatures.put(number,((String)wordMorphoSyntaxFeatures.get(mph)).substring(2, 3)); // s, p
			}

			//System.out.println("Debug: cat "+cat+" subcat "+subcat+" mph "+mph+ " mode "
			//		+mode+" person "+person+" gender "+gender+" number "+number);
		}
		// Get the Noun features
		else if (((String)wordMorphoSyntaxFeatures.get(cat)).equalsIgnoreCase("N")) {	
			// subcat P Proper C Common
			if (((String)wordMorphoSyntaxFeatures.get(mph)).length() == 2) { 
				wordMorphoSyntaxFeatures.put(gender,((String)wordMorphoSyntaxFeatures.get(mph)).substring(0, 1)); // m, f
				wordMorphoSyntaxFeatures.put(number,((String)wordMorphoSyntaxFeatures.get(mph)).substring(1, 2)); // s, p
			}
		}
		// Get the C conjonction features
		else if (((String)wordMorphoSyntaxFeatures.get(cat)).equalsIgnoreCase("C")) {				
			// subcat S subordinatin C Coordination
		}
		// Get the CL clitic features		
		else if (((String)wordMorphoSyntaxFeatures.get(cat)).equalsIgnoreCase("CL")) {
			// subcat refl, suj, obj 
			if (((String)wordMorphoSyntaxFeatures.get(mph)).length() == 3) {
				wordMorphoSyntaxFeatures.put(person,((String)wordMorphoSyntaxFeatures.get(mph)).substring(0, 1)); // 1, 2, 3
				wordMorphoSyntaxFeatures.put(gender,((String)wordMorphoSyntaxFeatures.get(mph)).substring(1, 2)); // m, f
				wordMorphoSyntaxFeatures.put(number,((String)wordMorphoSyntaxFeatures.get(mph)).substring(2, 3)); // s, p
			}
		}
		// Get the P preposition features		
		else if (((String)wordMorphoSyntaxFeatures.get(cat)).equalsIgnoreCase("P")) {
			// no subcat
		}		
		// Get the I Interjection  features		
		else if (((String)wordMorphoSyntaxFeatures.get(cat)).equalsIgnoreCase("I")) {
			// no subcat 
		}	
		// Get the PONCT features		
		else if (((String)wordMorphoSyntaxFeatures.get(cat)).equalsIgnoreCase("PONCT")) {
			// subcat S, W
		}
		// Get the Foreign word features		
		else if (((String)wordMorphoSyntaxFeatures.get(cat)).equalsIgnoreCase("ET")) {
			// no subcat 
		}
		// Get the Adjective features		
		else if (((String)wordMorphoSyntaxFeatures.get(cat)).equalsIgnoreCase("A")) {
			// subcat ind, qual, ord
			if (((String)wordMorphoSyntaxFeatures.get(mph)).length() == 2) {
				wordMorphoSyntaxFeatures.put(gender,((String)wordMorphoSyntaxFeatures.get(mph)).substring(0, 1)); // m, f
				wordMorphoSyntaxFeatures.put(number,((String)wordMorphoSyntaxFeatures.get(mph)).substring(1, 2)); // s, p
			}
		}
		// Get the Adverbe features		
		else if (((String)wordMorphoSyntaxFeatures.get(cat)).equalsIgnoreCase("ADV")) {
			// subcat int, neg
		}
		// Get the prefix features		
		else if (((String)wordMorphoSyntaxFeatures.get(cat)).equalsIgnoreCase("PREF")) {
			// no subcat
		}
		// Get the pronoun features		
		else if (((String)wordMorphoSyntaxFeatures.get(cat)).equalsIgnoreCase("PRO")) {
			// subcat rel, dem, ind, int, pers, poss, refl, neg
			if (((String)wordMorphoSyntaxFeatures.get(mph)).length() == 3) {
				wordMorphoSyntaxFeatures.put(person,((String)wordMorphoSyntaxFeatures.get(mph)).substring(0, 1)); // 1, 2, 3
				wordMorphoSyntaxFeatures.put(gender,((String)wordMorphoSyntaxFeatures.get(mph)).substring(1, 2)); // m, f
				wordMorphoSyntaxFeatures.put(number,((String)wordMorphoSyntaxFeatures.get(mph)).substring(2, 3)); // s, p
			}
		}
		// Get the Determinant features		
		else if (((String)wordMorphoSyntaxFeatures.get(cat)).equalsIgnoreCase("D")) {
			// subcat def, poss, dem, ind, card, excl, int, neg, part
			if (((String)wordMorphoSyntaxFeatures.get(mph)).length() == 2) {
				wordMorphoSyntaxFeatures.put(gender,((String)wordMorphoSyntaxFeatures.get(mph)).substring(0, 1)); // m, f
				wordMorphoSyntaxFeatures.put(number,((String)wordMorphoSyntaxFeatures.get(mph)).substring(1, 2)); // s, p
			}
			else if (((String)wordMorphoSyntaxFeatures.get(mph)).length() == 4) {
				wordMorphoSyntaxFeatures.put(person,((String)wordMorphoSyntaxFeatures.get(mph)).substring(0, 1)); // 1, 2, 3
				wordMorphoSyntaxFeatures.put(gender,((String)wordMorphoSyntaxFeatures.get(mph)).substring(1, 2)); // m, f
				wordMorphoSyntaxFeatures.put(number,((String)wordMorphoSyntaxFeatures.get(mph)).substring(2, 3)); // s, p
				wordMorphoSyntaxFeatures.put(possNumber,((String)wordMorphoSyntaxFeatures.get(mph)).substring(3, 4)); // s, p
				// D-poss-1mss MON ami
				// D-poss-2fps TES fleurs
				// D-poss-1msp NOTRE chien
				// D-poss-2fpp VOS fleurs
			}
		}		
		// Unknwon cat value	
		else {
			System.err.println("Debug: unknown cat value (is it a w?)  cat "+wordMorphoSyntaxFeatures.get(cat)+" subcat "+wordMorphoSyntaxFeatures.get(subcat)+" mph "+wordMorphoSyntaxFeatures.get(mph)+ " mode "
					+wordMorphoSyntaxFeatures.get(mood)+" person "+wordMorphoSyntaxFeatures.get(person)
					+" gender "+wordMorphoSyntaxFeatures.get(gender)
					+" number "+wordMorphoSyntaxFeatures.get(number));
		}

		//
		return wordMorphoSyntaxFeatures;
	}

	
	

}
